package es.udc.pfc.davidparedes.web.pages.user;

import java.util.Calendar;
import java.util.Date;

import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;

import es.udc.pfc.davidparedes.model.taskservice.TaskService;
import es.udc.pfc.davidparedes.model.unavailable.Unavailable;

import es.udc.pfc.davidparedes.model.userservice.UserService;
import es.udc.pfc.davidparedes.model.util.InstanceNotFoundException;

import es.udc.pfc.davidparedes.web.pages.user.VendorSchedule;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicy;
import es.udc.pfc.davidparedes.web.services.AuthenticationPolicyType;
import es.udc.pfc.davidparedes.web.util.UserSession;

@AuthenticationPolicy(AuthenticationPolicyType.VENDOR)
public class UpdateUnavailable {
    
    @SessionState(create=false)
    private UserSession userSession;
    
    @Inject
    private UserService userService;
    
    @Inject
    private TaskService taskService;

    @Property
    private Date startDate;
    
    @Property
    private Date endDate;
    
    @Property
    private String startTime;
    
    @Property
    private String endTime;
    
    @Property
    private Long updateUnavailableId;
    
    @Property
    private Long removeUnavailableId;
    
    
    void onActivate(Long id) throws InstanceNotFoundException {
    	
    	Unavailable unavailable = taskService.findUnavailable(id);
    	updateUnavailableId = removeUnavailableId = id;
    	
    	startDate = unavailable.getStart().getTime();
    	Integer startHour = unavailable.getStart().get(Calendar.HOUR_OF_DAY);
    	Integer startMin = unavailable.getStart().get(Calendar.MINUTE);
    	startTime = formatTime(startHour.toString()) + ":" + formatTime(startMin.toString());
    	
    	endDate = unavailable.getEnd().getTime();
    	Integer endHour = unavailable.getEnd().get(Calendar.HOUR_OF_DAY);
    	Integer endMin = unavailable.getEnd().get(Calendar.MINUTE);
    	endTime = formatTime(endHour.toString()) + ":" + formatTime(endMin.toString());
    	
    }

    
    Object onSuccessFromUpdateUnavailableForm() throws InstanceNotFoundException {
    	
    	Unavailable unavailable = taskService.findUnavailable(updateUnavailableId);
        
        if (userSession.getUserProfileId() == unavailable.getVendor().getUserProfileId()) {
        
	        String[] unavailableStartTime = startTime.split(":");
	        Integer unavailableStartHour = Integer.parseInt(unavailableStartTime[0]);
	        Integer unavailableStartMin = Integer.parseInt(unavailableStartTime[1]);
	        Calendar unavailableStart = Calendar.getInstance();
	        unavailableStart.setTime(startDate);
	        if ((0 <= unavailableStartHour) && (unavailableStartHour <= 23) && (0 <= unavailableStartMin) && (unavailableStartMin <= 59)) {
		        unavailableStart.set(Calendar.HOUR_OF_DAY, unavailableStartHour);
		        unavailableStart.set(Calendar.MINUTE, unavailableStartMin);
	        } else {
	        	return this;
	        }
	        
	        String[] unavailableEndTime = endTime.split(":");
	        Integer unavailableEndHour = Integer.parseInt(unavailableEndTime[0]);
	        Integer unavailableEndMin = Integer.parseInt(unavailableEndTime[1]);
	        Calendar unavailableEnd = Calendar.getInstance();
	        unavailableEnd.setTime(endDate);
	        if ((0 <= unavailableEndHour) && (unavailableEndHour <= 23) && (0 <= unavailableEndMin) && (unavailableEndMin <= 59)) {
		        unavailableEnd.set(Calendar.HOUR_OF_DAY, unavailableEndHour);
		        unavailableEnd.set(Calendar.MINUTE, unavailableEndMin);
	        } else {
	        	return this;
	        }
	        
	        if (unavailableStart.before(unavailableEnd)) {
				taskService.updateUnavailable(unavailable.getUnavailableId(), unavailableStart, unavailableEnd, unavailable.getVendor().getUserProfileId());
				return VendorSchedule.class;
	        }
	        
	    }
        
        return this;
        
    }
    
    Object onSuccessFromRemoveUnavailableForm() throws InstanceNotFoundException {
    	
    	Unavailable unavailable = taskService.findUnavailable(removeUnavailableId);
        
        if (userSession.getUserProfileId() == unavailable.getVendor().getUserProfileId()) {
        	taskService.removeUnavailable(unavailable.getUnavailableId());
        	return VendorSchedule.class;
        }
        return this;
    	
    }
    
    private String formatTime(String s) {
    	if (s.length()<2) {
    		s="0"+s;
    	}
    	return s;
    }

}
